package sys_ctl

import (
	"net/http"
	"ubt-rbac-service/internal/app/model"
	"ubt-rbac-service/internal/app/model/sys_mod"
	"ubt-rbac-service/internal/app/service/sys_serv"
	"ubt-rbac-service/internal/app/util"
	"ubt-rbac-service/internal/core/convert"
	"ubt-rbac-service/internal/core/parse"
)

type sysNote struct{}

var SysNoteCtl sysNote

// Process 处理消息
func (c *sysNote) Process(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &sys_mod.SysNoteProReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*sys_mod.SysNoteProReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.SnuIds = util.InputFilter(req.SnuIds, true)
	result := sys_serv.SysNoteServ.Process(req)
	if result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}

// One 获取消息详情
func (c *sysNote) One(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	// 过滤关键参数值中的非法字符
	noteId := u.Get("noteId")
	noteId = util.InputFilter(noteId, true)
	if noteId == "" || len(noteId) != 21 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的编号参数为空或不正确，请重试！"})
		return
	}
	result := sys_serv.SysNoteServ.One(noteId)
	if result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
	} else {
		util.ToJson(w, &model.ResJson{Code: 404, Msg: result.RetMsg})
	}
}

// Page 获取消息分页列表
func (c *sysNote) Page(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	schName := u.Get("schName")
	beginTimes := u.Get("beginTime")
	endTimes := u.Get("endTime")
	limits := u.Get("limit")
	offsets := u.Get("offset")
	schName = util.InputFilter(schName, false)
	limits = util.InputFilter(limits, true)
	offsets = util.InputFilter(offsets, true)
	limit := convert.Str2u8(limits)
	offset := convert.Str2u32(offsets)
	beginTime := convert.Str2int(beginTimes)
	endTime := convert.Str2int(endTimes)
	// 先统计信息的数量
	req := &sys_mod.SysNotePageReq{UserId: userInfo.UserId, SchName: schName, BeginTime: beginTime, EndTime: endTime}
	if result := sys_serv.SysNoteServ.Count(req); result != nil && result.RetCode == 1001 && result.RetData.(int64) > 0 {
		res := &sys_mod.SysNotePageRes{Total: result.RetData.(int64)}
		// 再查询具体的信息列表
		req.Limit = limit
		req.Offset = offset
		if result = sys_serv.SysNoteServ.Page(req); result != nil && result.RetCode == 1001 {
			res.List = result.RetData
			util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: res})
			return
		}
	}
	util.ToJson(w, &model.ResJson{Code: 200, Msg: "暂无内容"})
}

// New 查询是否存在未读消息
func (c *sysNote) New(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	result := sys_serv.SysNoteServ.New(userInfo.UserId)
	if result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
	} else {
		util.ToJson(w, &model.ResJson{Code: 404, Msg: "暂无内容"})
	}
}

// Read 已读消息
func (c *sysNote) Read(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为结构体，并且校验请求的参数，如参数有误则会返回具体的错误结果
	vo, errStr := parse.Parse(r, &sys_mod.SysNoteReadReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*sys_mod.SysNoteReadReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	req.SnuId = util.InputFilter(req.SnuId, true)
	if req.SnuId == "" || len(req.SnuId) != 21 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，您请求的参数不正确，请重试！"})
		return
	}
	result := sys_serv.SysNoteServ.Read(req.SnuId)
	if result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}
